/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * HanhKhachFrm.java
 *
 * Created on Jun 2, 2011, 11:53:34 AM
 */
package gui;

import dao.HanhKhachDAO;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
import pojo.HanhKhachPOJO;

/**
 *
 * @author b2
 */
public class HanhKhachFrm extends javax.swing.JFrame {
   
  

    /** Creates new form HanhKhachFrm */
    public HanhKhachFrm() {
        initComponents();
    }


    public void initHanhKhach() {
        ArrayList<HanhKhachPOJO> ds = HanhKhachDAO.LayDanhSachHanhKhach();
        DefaultTableModel dtm = new DefaultTableModel();
        dtm.addColumn("Mã hành khách");
        dtm.addColumn("Tên hành khách");
        dtm.addColumn("CMND");
        dtm.addColumn("Điện thoại");
        dtm.setNumRows(ds.size());
        for (int i = 0; i < ds.size(); i++) {
            HanhKhachPOJO hk = ds.get(i);
            dtm.setValueAt(hk.getMaHanhKhach(), i, 0);
            dtm.setValueAt(hk.getTenHanhKhach(), i, 1);
            dtm.setValueAt(hk.getCMND(), i, 2);
            dtm.setValueAt(hk.getDienThoai(), i, 3);
        }
        this.jtbHanhKhach.setModel(dtm);
        this.jtbHanhKhach.changeSelection(0, 0, false, false);
    }

    public void timkiemTheoMa() {
        String maHanhKhach = this.txtMaHanhKhach.getText();
        ArrayList<HanhKhachPOJO> ds = HanhKhachDAO.TimKiemHanhKhachTheoMa(maHanhKhach);
        DefaultTableModel dtm = new DefaultTableModel();
        dtm.addColumn("Mã hành khách");
        dtm.addColumn("Tên hành khách");
        dtm.addColumn("CMND");
        dtm.addColumn("Điện thoại");
        dtm.setNumRows(ds.size());
        for (int i = 0; i < ds.size(); i++) {
            HanhKhachPOJO hk = ds.get(i);
            dtm.setValueAt(hk.getMaHanhKhach(), i, 0);
            dtm.setValueAt(hk.getTenHanhKhach(), i, 1);
            dtm.setValueAt(hk.getCMND(), i, 2);
            dtm.setValueAt(hk.getDienThoai(), i, 3);
        }
        this.jtbHanhKhach.setModel(dtm);
        this.jtbHanhKhach.changeSelection(0, 0, false, false);

    }

    public void timkiemTheoTen() {
        String tenHanhKhach = this.txtTenHanhKhach.getText();

        ArrayList<HanhKhachPOJO> ds = HanhKhachDAO.TimKiemHanhKhachTheoTen(tenHanhKhach);
        DefaultTableModel dtm = new DefaultTableModel();
        dtm.addColumn("Mã hành khách");
        dtm.addColumn("Tên hành khách");
        dtm.addColumn("CMND");
        dtm.addColumn("Điện thoại");
        System.out.println(tenHanhKhach);
        dtm.setNumRows(ds.size());
        for (int i = 0; i < ds.size(); i++) {
            HanhKhachPOJO hk = ds.get(i);
            dtm.setValueAt(hk.getMaHanhKhach(), i, 0);
            dtm.setValueAt(hk.getTenHanhKhach(), i, 1);
            dtm.setValueAt(hk.getCMND(), i, 2);
            dtm.setValueAt(hk.getDienThoai(), i, 3);
        }
        this.jtbHanhKhach.setModel(dtm);
        this.jtbHanhKhach.changeSelection(0, 0, false, false);

    }

    public void chonHanhKhach() {
        int dong = this.jtbHanhKhach.getSelectedRow();
        String maHK = (String) this.jtbHanhKhach.getValueAt(dong, 0);
        BanVeFrm frm = new BanVeFrm();
        frm.laytMaHanhKhach(maHK);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtMaHanhKhach = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbHanhKhach = new javax.swing.JTable();
        btnThemMoi = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        txtTenHanhKhach = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Danh sách hành khách");
        setBounds(new java.awt.Rectangle(300, 300, 0, 0));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel1.setText("Danh sách hành khách");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh sách hành khách"));
        jPanel1.setEnabled(false);

        jLabel2.setText("Mã hành khách");

        txtMaHanhKhach.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtMaHanhKhachCaretUpdate(evt);
            }
        });

        jtbHanhKhach.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtbHanhKhach);

        btnThemMoi.setText("Thêm mới");
        btnThemMoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemMoiActionPerformed(evt);
            }
        });

        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        jLabel3.setText("Tên hành khách");

        txtTenHanhKhach.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtTenHanhKhachCaretUpdate(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 384, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(30, 30, 30)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtMaHanhKhach)
                            .addComponent(txtTenHanhKhach, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE))))
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(88, 88, 88)
                .addComponent(btnThemMoi)
                .addGap(46, 46, 46)
                .addComponent(btnThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(102, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtTenHanhKhach, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtMaHanhKhach, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnThemMoi)
                    .addComponent(btnThoat))
                .addGap(76, 76, 76))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(75, 75, 75))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnThoatActionPerformed

    private void txtTenHanhKhachCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtTenHanhKhachCaretUpdate
        // TODO add your handling code here:
        this.timkiemTheoTen();
    }//GEN-LAST:event_txtTenHanhKhachCaretUpdate

    private void txtMaHanhKhachCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtMaHanhKhachCaretUpdate
        // TODO add your handling code here:
        this.timkiemTheoMa();
    }//GEN-LAST:event_txtMaHanhKhachCaretUpdate

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        this.initHanhKhach();
    }//GEN-LAST:event_formWindowOpened

    private void btnThemMoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemMoiActionPerformed
        // TODO add your handling code here:
        ThemHanhKhachFrm frm = new ThemHanhKhachFrm();
        frm.setVisible(true);
    }//GEN-LAST:event_btnThemMoiActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new HanhKhachFrm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnThemMoi;
    private javax.swing.JButton btnThoat;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jtbHanhKhach;
    private javax.swing.JTextField txtMaHanhKhach;
    private javax.swing.JTextField txtTenHanhKhach;
    // End of variables declaration//GEN-END:variables
}
